/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.View;

import java.awt.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 *
 * @author admin
 */
public class PanelTambahFotoAnggota extends JPanel {

    private JLabel labelKodeAnggota;
    private JTextField textKodeAnggota;
    private JButton tombolSimpan, tombolCariGambar;
    private File file;
    private BufferedImage image;
//    private JPanel panelTombol;

    public PanelTambahFotoAnggota() {
        inisialisasi();
        try {

            file = new File("C:sunset.jpg");
            image = ImageIO.read(file);
//            image.getGraphics();
        } catch (IOException ex) {
//            ex.printStackTrace();
            Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void paintComponents(Graphics g) {

        g.drawImage(image,
                0, 0, 400, 300,
                0, 0, image.getWidth(null), image.getHeight(null),
                null);

//        try {
//            setImage(ImageIO.read(file));
//            g.drawImage(getImage(), 50, 50, 300, 200, 0, 0, getImage().getWidth(null), getImage().getHeight(null), null);
//        } catch (IOException ex) {
//            Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
//        }
    }

    public void inisialisasi() {
        setLayout(new BorderLayout());

        JPanel panelTombol = new JPanel();
        panelTombol.setLayout(new FlowLayout());

        labelKodeAnggota = new JLabel("Kode");
        panelTombol.add(labelKodeAnggota);

        textKodeAnggota = new JTextField();
        textKodeAnggota.setColumns(10);
        panelTombol.add(textKodeAnggota);
        textKodeAnggota.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });

        tombolCariGambar = new JButton("CariGambar");
        panelTombol.add(tombolCariGambar);
        tombolCariGambar.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tombolCariGambarAction();
            }
        });

        tombolSimpan = new JButton("Simpan");
        panelTombol.add(tombolSimpan);
        tombolSimpan.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                tombolSimpanAction();
            }
        });

        add(panelTombol, BorderLayout.SOUTH);
    }

    public void tombolCariGambarAction() {
        JFileChooser chooser = new JFileChooser();
        int status = chooser.showOpenDialog(this);
        if (status == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            repaint();
        }
    }

    public void tombolSimpanAction() {
        try {
            FileOutputStream outStream = null;
            File fileBaru = new File(textKodeAnggota.getText());
            outStream = new FileOutputStream(fileBaru);
            FileInputStream input = new FileInputStream(file);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(PanelTambahFotoAnggota.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }
}
